﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel
    <DesignerSerializer(GetType(ActivityChangeActionMarkupSerializer), GetType(WorkflowMarkupSerializer))> _
    Public MustInherit Class ActivityChangeAction
        Inherits WorkflowChangeAction
        ' Methods
        Protected Sub New()
            Me.ownerActivityDottedPath = String.Empty
        End Sub

        Protected Sub New(ByVal compositeActivity As CompositeActivity)
            Me.ownerActivityDottedPath = String.Empty
            If (compositeActivity Is Nothing) Then
                Throw New ArgumentNullException("compositeActivity")
            End If
            Me.ownerActivityDottedPath = compositeActivity.DottedPath
        End Sub

        Protected Friend Overrides Function ValidateChanges(ByVal contextActivity As Activity) As ValidationErrorCollection
            If (contextActivity Is Nothing) Then
                Throw New ArgumentNullException("contextActivity")
            End If
            Dim errors As New ValidationErrorCollection
            Dim activity As CompositeActivity = [TryCast](contextActivity.TraverseDottedPathFromRoot(Me.OwnerActivityDottedPath),CompositeActivity)
            If ((Not activity Is Nothing) AndAlso WorkflowChanges.IsActivityExecutable(activity)) Then
                Dim validator As Validator
                For Each validator In ComponentDispenser.CreateComponents(activity.GetType, GetType(ActivityValidatorAttribute))
                    Dim item As ValidationError = validator.ValidateActivityChange(activity, Me)
                    If (Not item Is Nothing) Then
                        errors.Add(item)
                    End If
                Next
            End If
            Return errors
        End Function


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Property OwnerActivityDottedPath As String
            Public Get
                Return Me.ownerActivityDottedPath
            End Get
            Friend Set(ByVal value As String)
                Me.ownerActivityDottedPath = value
            End Set
        End Property


        ' Fields
        Private ownerActivityDottedPath As String
    End Class
End Namespace

